/* 
 *  Copyright (C) 2006 Dip. Ing. dell'Informazione, University of Pisa, Italy
 *  http://info.iet.unipi.it/~cng/ns2measure/ns2measure.html
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA, USA
 */

/**
   project: measure
   filename: main.cc
	author: C. Cicconetti <c.cicconetti@iet.unipi.it>
	year: 2007
   affiliation:
      Dipartimento di Ingegneria dell'Informazione
	   University of Pisa, Italy
   description:
	   Utility to check if saved data reaches the confidence level.
*/

#include <config.h>
#include <measure.h>
#include <input.h>
#include <output.h>

#include <cstdlib>

#include <iostream>
#include <string>
#include <set>

using namespace std;

void
printLicense () {
	cout
<< "Copyright (C) 2006 Dip. Ing. dell'Informazione, University of Pisa, Italy\n"
<< "http://info.iet.unipi.it/~cng/ns2measure/ns2measure.html\n"
<< "\n"
<< "This program is free software; you can redistribute it and/or modify\n"
<< "it under the terms of the GNU General Public License as published by\n"
<< "the Free Software Foundation; either version 2 of the License, or\n"
<< "(at your option) any later version.\n"
<< "\n"
<< "This program is distributed in the hope that it will be useful,\n"
<< "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
<< "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
<< "GNU General Public License for more details.\n"
<< "\n"
<< "You should have received a copy of the GNU General Public License\n"
<< "along with this program; if not, write to the Free Software\n"
<< "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA, USA\n";
	exit (0);
}

void
printUsage (const char* progname) {
	cout << "usage: " << progname
		  << " savefile [-hv] [-c cl] [-t th] <-r config | metrics...>\n"
		  << "options:\n"
		  << "   -c cl       use confidence level 'cl' (default = 0.95)\n"
		  << "   -t th       use confidence threshold 'th' (default = 0.05)\n"
		  << "   -r config   use configuration file 'config'\n"
		  << "   -v          print license information\n"
		  << "   -h          print this help\n";
	exit (0);
}

int
main (int argc, char* argv[]) {
	int ch;   // used by getopt
	const char* progname = argv[0];
	string saveFileName;
	string configFileName;
	double cl = 0.95;    // confidence level
	double th = 0.05;    // confidence threshold

	while ( ( ch = getopt (argc, argv, "c:r:t:hv") ) != -1 ) {
		switch (ch) {
			case 'c':
				cl = atof (optarg);
				break;
			case 't':
				th = atof (optarg);
				break;
			case 'r':
				configFileName = string (optarg);
				break;
			case 'v':
				printLicense ();          // does not return
			case 'h':
			default:
				printUsage(progname);     // does not return
		}
	}

	argc -= optind;
	argv += optind;

	if ( ( argc != 1 && ! configFileName.empty() ) ||
		  ( argc <= 1 &&   configFileName.empty() ) )
		printUsage(progname);  // does not return
	
	try {
		Configuration conf;
		Metrics metrics;

		bool ok = false;

		// read configuration either from a file
		if ( ! configFileName.empty() ) {
			conf.parse (configFileName);
			Input input (conf, metrics);
			ok = input.checkSavedData();

		// or from the command-line
		} else {
			set<string> relevant;
			for ( int i = 1 ; i < argc ; i++ ) {
				relevant.insert (argv[i]);
			}

			Input input (conf, metrics);
			input.recoverData (argv[0]);
			ok = metrics.checkConfidence (relevant, cl, th);
		}

		if ( ok ) cout << "ok" << endl;
		else      cout << "no" << endl;

	} catch (Object& obj) {
		cout << "err" << endl;
	}

	exit (1);
}
